﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "LabelDefinition.h"
#include "ControlDefinition.h"
#include "GbmpNew.h"
#include "ControlDefinitionChangeEvent.h"
#include "ILabelHandler.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace gcmp
{

LabelDefinition::LabelDefinition(const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip)
    : m_pControlDefinition(NEW_AS_OWNER_PTR(ControlDefinition, id, caption, tooltip))
    //,  m_pHandler(nullptr)
{
    m_pLabelDefinitionChangeEvent = NEW_AS_OWNER_PTR(ControlDefinitionChangeEvent, L"ControlDefinitionChangeEvent", this);
}

LabelDefinition::~LabelDefinition()
{
}

std::wstring LabelDefinition::GetId() const
{
    return m_pControlDefinition->GetId();
}

std::wstring LabelDefinition::GetCaption() const
{
    return m_pControlDefinition->GetCaption();
}

std::wstring LabelDefinition::GetTooltip() const
{
    return m_pControlDefinition->GetTooltip();
}

bool LabelDefinition::SetCaption(const std::wstring& caption)
{
    if (GetCaption() != caption)
    {
        m_pControlDefinition->SetCaption(caption);

        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pLabelDefinitionChangeEvent->Emit(&arguments);
    
    }
    return true;
}

bool LabelDefinition::SetTooltip(const std::wstring& tooltip)
{
    if (GetTooltip() != tooltip)
    {
        m_pControlDefinition->SetTooltip(tooltip);

        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pLabelDefinitionChangeEvent->Emit(&arguments);

    }
    return true;
}

IControlDefinitionChangeEvent* LabelDefinition::GetControlDefinitionChangeEvent() const
{
    return const_cast<ControlDefinitionChangeEvent*>(m_pLabelDefinitionChangeEvent.get());
}

bool LabelDefinition::IsVisible() const
{
    if (m_pHandler)
    {
        return m_pHandler->IsVisible();
    }
    return m_pControlDefinition->IsVisible();
}

bool LabelDefinition::IsEnabled() const
{
    return m_pControlDefinition->IsEnabled();
}

bool LabelDefinition::SetVisible(bool visible)
{
    bool res = m_pControlDefinition->SetVisible(visible);
    if (res)
    {
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pLabelDefinitionChangeEvent->Emit(&arguments);
    }
    return res;
}

bool LabelDefinition::SetControlHandler(OwnerPtr<ILabelHandler> pControlHandler)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pControlHandler, L"pControlHandler为空",L"GDMPLab",L"2024-03-30");
    bool visible = IsVisible();
    bool enabled = IsEnabled();
    ILabelHandler* pHandler = pControlHandler.get();
    m_pHandler = TransferOwnership(pControlHandler);

    if (visible != pHandler->IsVisible())
    {
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pLabelDefinitionChangeEvent->Emit(&arguments);
    }
    return true;
}

bool LabelDefinition::BlockControlHandler(bool needBlock)
{
    return false;
}

}   // end namespace gcmp
